package com.relife.core.service;

import java.util.Date;

import com.relife.common.http.BrowserType;
import com.relife.common.http.BrowserUtils;
import com.relife.common.http.HttpUtil;
import com.relife.common.model.Client;
import com.relife.common.model.ClientManager;
import com.relife.core.entity.LogEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service("logService")
@Transactional
public class LogService extends BaseService {

	public void optLog(LogEntity log) {
		Client client = ClientManager.getInstance().getClientSession();
		if (client != null) {
			log.setOptUser(client.getUser().getName());
			log.setDepCode(client.getDepart().getDepartCode());
			log.setUserCode(client.getUser().getId());
			log.setOptDate(new Date());

			if (HttpUtil.getRequest() != null) {
				log.setOptIp(HttpUtil.getIpAddr(HttpUtil.getRequest()));
				log.setOptUrl(HttpUtil.getRequest().getServletPath());
				BrowserType type = BrowserUtils.getBrowserType(HttpUtil.getRequest());
				log.setBrowser(type == null ? null : type.toString());
			}
			this.save(log);
		}
	}
}
